package iblink.junit;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import iblink.adapter.IBFacade;
import iblink.core.HistoricalData;
import iblink.core.Option;
import iblink.core.ScannerArgs;
import iblink.core.ScannerArgs.ScanCode;
import iblink.core.ScannerData;
import iblink.core.Stock;
import iblink.core.ScannerArgs.Instrument;
import iblink.core.ScannerArgs.LocationCode;
import iblink.util.Constants.BarSize;
import iblink.util.Constants.DataType;
import iblink.util.Constants.Duration;
import iblink.util.Constants.Right;

import org.junit.Assert;
import org.junit.Test;

import com.ib.client.ContractDetails;

import packutil.DateUtil;

public class TestMain extends Assert {

	// TODO: rename the testing classes to something that makes sense..
	// they all sound the same

	@Test
	public void TestHistoricalDataOneMinuteBars() {

		IBFacade ibFacade = TestHelper.getIBFacade();
		Stock sIBM = new Stock("IBM", null, null, null, null, null);

		// get 1-minute bars for the last trading day
		List<HistoricalData> histData = ibFacade.getHistoricalData(sIBM,
				new Date(), Duration.ONE_DAY, BarSize.ONE_MIN, DataType.TRADES,
				true);

		for (HistoricalData hd : histData) {
			System.out.println(hd);
		}

		// 290 means there was a trade in every 1-minute period throughout
		// the day, which is what we expect for a high volume stock like IBM
		System.out.println(histData.size());
		assertTrue(histData.size() == 390);
	}

	@Test
	public void TestGettingOptionsForStock() {

		Set<Option> optionsSet = new HashSet<Option>();

		IBFacade ibFacade = TestHelper.getIBFacade();
		Stock sMSFT = new Stock("MSFT", null, null, null, null, null);
		List<Option> options = ibFacade.getOptions(sMSFT);

		for (Option o : options) {
			System.out.println(o);
			// test for duplicates
			assertFalse(optionsSet.contains(o));
			optionsSet.add(o);
		}

		// msft should have a lot of options..
		assertTrue(options.size() > 50);
	}

	@Test
	public void TestGettingHistoricalDataForOption() {

		IBFacade ibFacade = TestHelper.getIBFacade();
		Stock sMSFT = new Stock("MSFT", null, null, null, null, null);
		Option option = new Option(sMSFT, "MSFT  120421C00032000", Right.CALL,
				27.00, DateUtil.parseDate("20111118", "yyyyMMdd"));

		List<HistoricalData> histData = ibFacade.getHistoricalData(option,
				new Date(), Duration.ONE_DAY, BarSize.ONE_HOUR,
				DataType.TRADES, true);

		for (HistoricalData h : histData) {
			System.out.println(h);
		}
		System.out.println(option);
	}

	@Test
	public void TestRequestScannerData() {

		ScannerArgs sa = new ScannerArgs(LocationCode.STK_AMEX, Instrument.STK,
				ScanCode.HIGH_OPT_VOLUME_PUT_CALL_RATIO);

		sa.numberOfRows(15);

		IBFacade ibFacade = TestHelper.getIBFacade();
		List<ScannerData> sd = ibFacade.getScannerData(sa);
		assertTrue(sd.size() == 15);
	}

	@Test
	public void TestGetContractDetailsForStock() {

		IBFacade ibFacade = TestHelper.getIBFacade();
		ContractDetails d = ibFacade.getContractDetailsForStock("IBM");
		assertTrue(d != null);
		assertTrue("STK".equals(d.m_summary.m_secType));
		assertTrue("Computers".equals(d.m_category));
		assertTrue("Technology".equals(d.m_industry));
		assertTrue("Computer Services"
				.equals(d.m_subcategory = "Computer Services"));
		assertTrue("NYSE".equals(d.m_summary.m_primaryExch));
		assertTrue("INTL BUSINESS MACHINES CORP".equals(d.m_longName));
	}

	@Test
	public void TestGetStock() {

		IBFacade ibFacade = TestHelper.getIBFacade();

		// subtest 1) get stock that exists
		Stock s = ibFacade.getStock("IBM");
		assertTrue(s != null);
		assertTrue("IBM".equals(s.getSymbol()));
		assertTrue("NYSE".equals(s.getPrimaryExchange()));
		assertTrue("Computers".equals(s.getCategory()));
		assertTrue("Technology".equals(s.getIndustry()));
		assertTrue("INTL BUSINESS MACHINES CORP".equals(s.getLongName()));

		// subtest 2) try get stock that doesn't exist
		Stock s2 = ibFacade.getStock("MADEUPSYMBOL");
		assertTrue(s2 == null);
	}
}